package org.infodavid.persistence.impl.hibernate.usertype;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
import org.infodavid.common.util.LocaleUtil;

/**
 * The Class LocaleLanguageUserType.
 */
@SuppressWarnings("rawtypes")
public class LocaleLanguageUserType implements UserType {

  /** The Constant SQL_TYPES. */
  private static final int[] SQL_TYPES = {
    Types.VARCHAR
  };

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#sqlTypes()
   */
  @Override
  public int[] sqlTypes() {
    return SQL_TYPES;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#returnedClass()
   */
  @Override
  public Class returnedClass() {
    return Locale.class;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
   */
  @Override
  public boolean equals(final Object x, final Object y) throws HibernateException {
    final boolean result;

    if (x == y) {
      result = true;
    }
    else if (x == null || y == null) {
      result = false;
    }
    else {
      result = x.equals(y);
    }

    return result;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[],
   * java.lang.Object)
   */
  public Object nullSafeGet(final ResultSet resultSet, final String[] names, final Object owner)
      throws HibernateException, SQLException {
    final String value = resultSet.getString(names[0]);
    final Locale result;

    if (value == null) {
      result = null;
    }
    else {
      result = LocaleUtil.getInstance().getLocale(value);
    }

    return result;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object,
   * int)
   */
  public void nullSafeSet(final PreparedStatement statement, final Object value, final int index)
      throws HibernateException, SQLException {
    if (value instanceof Locale) {
      statement.setString(index, ((Locale)value).getLanguage());
    }
    else {
      statement.setNull(index, Types.VARCHAR);
    }
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
   */
  @Override
  public Object deepCopy(final Object value) throws HibernateException {
    return value;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#isMutable()
   */
  @Override
  public boolean isMutable() {
    return false;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
   */
  @Override
  public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
    return cached;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
   */
  @Override
  public Serializable disassemble(final Object value) throws HibernateException {
    return (Serializable)value;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
   */
  @Override
  public int hashCode(final Object x) throws HibernateException {
    return x instanceof Locale ? ((Locale)x).getLanguage().hashCode() : 0;
  }

  /*
   * (non-Javadoc)
   * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object,
   * java.lang.Object)
   */
  @Override
  public Object replace(final Object original, final Object target, final Object owner)
      throws HibernateException {
    return target;
  }

  /**
   * See super class or interface. (non-Javadoc)
   * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[],
   *      org.hibernate.engine.spi.SessionImplementor, java.lang.Object)
   */
  @Override
  public Object nullSafeGet(ResultSet arg0, String[] arg1, SessionImplementor arg2, Object arg3)
      throws HibernateException, SQLException {
    return nullSafeGet(arg0, arg1, arg3);
  }

  /**
   * See super class or interface. (non-Javadoc)
   * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object,
   *      int, org.hibernate.engine.spi.SessionImplementor)
   */
  @Override
  public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2, SessionImplementor arg3)
      throws HibernateException, SQLException {
    nullSafeSet(arg0, arg1, arg2);
  }
}
